package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.EduMoneyAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.bean.ResterPostionBeans;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ViewBgUtil;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 修改额度
 */
public class EduMoneyFragment extends BaseFragment {
    public static EduMoneyFragment newInstance(String cardId, String payLimit,String useLimit, String phone, String realName, String cardType) {
        EduMoneyFragment eduMoneyFragment = new EduMoneyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("cardId", cardId);
        bundle.putString("payLimit", payLimit);
        bundle.putString("useLimit", useLimit);
        bundle.putString("phone", phone);
        bundle.putString("realName", realName);
        bundle.putString("cardType", cardType);
        eduMoneyFragment.setArguments(bundle);
        return eduMoneyFragment;
    }

    @BindView(R.id.tv_memeber_yue)
    TextView tvMemberYue;
    @BindView(R.id.tv_memeber_use_yue)
    TextView tv_memeber_use_yue;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.tv_memeber_phone)
    TextView tvMemverPhone;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    @BindView(R.id.tv_nick)
    TextView tv_nick;
    @BindView(R.id.img_card)
    ImageView img_card;
    @BindView(R.id.ly_main)
    LinearLayout ly_main;
    @BindView(R.id.rl_edu_money)
    RecyclerView rlEduMoney;
    @BindView(R.id.tv_reghter_amounts)
    EditText etAmount;
    private EduMoneyAdapter eduMoneyAdapter;
    private String cardId;

    @Override
    protected int initLayout() {
        return R.layout.fg_edumoney;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.ModifyQuota), getActivity());
        ly_main.setBackground(ViewBgUtil.setBg(15, Color.parseColor(ConfigureFile.ColorWhite)));
        eduMoneyAdapter = new EduMoneyAdapter(getContext(), null, 0);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        rlEduMoney.setLayoutManager(gridLayoutManager);
        eduMoneyAdapter.setOnItemClickListener((v, position) -> {
            String amount = eduMoneyAdapter.getListData().get(position).getAmountStr();
            etAmount.setText(amount);
        });
        rlEduMoney.setAdapter(eduMoneyAdapter);
        etAmount.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("EDITTEXT", "beforeTextChanged: 输入过程中执行该方法");

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = String.valueOf(s);
                boolean isHave = false;
                int num = 0;
                for (int i = 0; i < eduMoneyAdapter.getListData().size(); i++) {
                    if (String.valueOf(text).equals(eduMoneyAdapter.getListData().get(i).getAmountStr())) {
                        isHave = true;
                        num = i;
                    }
                }
                if (isHave) {
                    eduMoneyAdapter.setPosition(num);
                } else {
                    eduMoneyAdapter.setPosition(-1);
                }

            }
        });
        tvPay.setOnClickListener(view1 -> {
            String eduAmount = etAmount.getText().toString().trim();
            if (StringUtils.isEmpty(eduAmount)) {
                ToastUtils.showToast(getContext(), "请输入自定义额度！");
                return;
            }

            if ("0".equals(eduAmount)) {
                ToastUtils.showToast(getContext(), "请输入正确的额度！");
                return;
            }

            updateCardQuota(eduAmount);
        });

    }

    @Override
    protected void initData(Context mContext) {
        cardId = getArguments().getString("cardId");
        String payLimit = getArguments().getString("payLimit");
        String useLimit = getArguments().getString("useLimit");
        String phone = getArguments().getString("phone");
        String realName = getArguments().getString("realName");
        String cardType = getArguments().getString("cardType");
        setUI(payLimit,useLimit, phone, realName, cardType);
    }

    @Override
    protected void loadMethod() {
        getOilsRegsterData();
    }

    private void setUI(String payLimit,String useLimit, String phone, String realName, String cardType) {
        if (!StringUtils.isEmpty(payLimit)) {
            String amounts = ConverterHelper.toBigDecimal(payLimit).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            tvMemberYue.setText("￥" + amounts);
            tv_memeber_use_yue.setText("已用￥" + useLimit);
        }
        tvMemberName.setText("" + realName);
        tvMemverPhone.setText("" + phone);
        if (!StringUtils.isEmpty(cardType)) {
            int img;
            String nick;
            if ("1".equals(cardType)) {
                img = R.mipmap.mother_card;
                nick = "父母卡";
            } else if ("3".equals(cardType)) {
                img = R.mipmap.qingqing_card;
                nick = "伴侣卡";
            } else if ("2".equals(cardType)) {
                img = R.mipmap.son_card;
                nick = "子女卡";
            } else if ("4".equals(cardType)) {
                img = R.mipmap.yuangong_card;
                nick = "企业卡";
            } else {
                img = R.mipmap.other_card;
                nick = "其他卡";
            }
            img_card.setImageResource(img);
            tv_nick.setText(nick);
        }
    }

    /**
     * 添加充值档位
     */
    private void getOilsRegsterData() {

        List<ResterPostionBeans> selectRegsterMoneyList = new ArrayList<>();

        ResterPostionBeans regsterPositionNewBean1 = new ResterPostionBeans();
        regsterPositionNewBean1.setAmountStr("1000");
        regsterPositionNewBean1.setAmount(100000);

        ResterPostionBeans regsterPositionNewBean2 = new ResterPostionBeans();
        regsterPositionNewBean2.setAmountStr("2000");
        regsterPositionNewBean2.setAmount(200000);

        ResterPostionBeans regsterPositionNewBean3 = new ResterPostionBeans();
        regsterPositionNewBean3.setAmountStr("3000");
        regsterPositionNewBean3.setAmount(300000);

        ResterPostionBeans regsterPositionNewBean4 = new ResterPostionBeans();
        regsterPositionNewBean4.setAmountStr("5000");
        regsterPositionNewBean4.setAmount(500000);
        selectRegsterMoneyList.add(regsterPositionNewBean1);
        selectRegsterMoneyList.add(regsterPositionNewBean2);
        selectRegsterMoneyList.add(regsterPositionNewBean3);
        selectRegsterMoneyList.add(regsterPositionNewBean4);
        eduMoneyAdapter.setListData(selectRegsterMoneyList);
        eduMoneyAdapter.notifyDataSetChanged();
    }

    private void updateCardQuota(String eduAmount) {
        String payLimitAmount = ConverterHelper.toBigDecimal(eduAmount).multiply(ConverterHelper.toBigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).toString();
        RequestUtils.UpdateCardQuota(getContext(), cardId, payLimitAmount, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {
                ToastUtils.showToast(getContext(), "操作成功!");
                EventBus.getDefault().post(new MessageEvent(MessageEvent.CARD));
                EventBus.getDefault().post(new MessageEvent(MessageEvent.UPDATEEDU,payLimitAmount));//操作成功后更新额度值
                goBack(getActivity());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
                goBack(getActivity());
            }
        });
    }

}
